//
//  BaseNavigationController.swift
//  个人微博
//
//  Created by 一介布衣 on 16/2/26.
//  Copyright © 2016年 HUAMANLOU. All rights reserved.
//

import UIKit

class BaseNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 一旦自定义了返回按钮，原有的返回手势就会自动的失效
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    // 重写push方法
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        print("xiixixiixx")
        
        // 获取子视图控制器的数量
        let count = childViewControllers.count
        print(childViewControllers)
        print("push:\(count)")
        if count != 0 {
            // push出来的控制器
            viewController.hidesBottomBarWhenPushed = true
          
            // 自定义返回按钮
            let btn = UIButton(backImageName: nil, text: "返回", textColor: UIColor.darkGrayColor(), imageName: "navigationbar_back_withtext", fontSize: 14)
           viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
            btn.addTarget(self, action: "back", forControlEvents: .TouchUpInside)
        }
        // 首页
        // BaseNavigationController(rootViewController: vc)其实也会调用push的操作
        super.pushViewController(viewController, animated: true)
    }
    
    @objc private func back() {
        self.popViewControllerAnimated(true)
    }
    
    // 实现手势的协议方法
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        print("hahhahah")
//        if childViewControllers.count > 1 {
//            print("heiheiheie")
//            return true
//        }
//        
//        return false
//        print(childViewControllers.count)
//        print(childViewControllers)
        return childViewControllers.count > 1
    }

}
